from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from starlette.config import Config

# SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
config = Config(".env")  # 解析 .env 文件中的环境变量
DB_NAME = config("DB_NAME")
DB_USERNAME = config("DB_USERNAME")
DB_PASSWORD = config("DB_PASSWORD")
DB_HOST = config("DB_HOST")
DB_PORT = config("DB_PORT")


DB_URI = f'mysql+pymysql://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}'
SQLALCHEMY_DATABASE_URL = DB_URI

engine = create_engine(
    # .check_same_thread仅用于SQLite，在其他数据库不需要它。
    # SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    # SQLALCHEMY_TRACK_MODIFICATIONS = False,
    SQLALCHEMY_DATABASE_URL, connect_args={}
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

